package com.itheima.mp.domain.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "分页查询条件实体")
public class PageQuery {
    @Schema(description = "当前页码")
    private Long pageNo;
    @Schema(description = "每页记录数")
    private Long pageSize;
    @Schema(description = "排序字段")
    private String sortBy;
    @Schema(description = "是否升序")
    private Boolean isAsc;

    public <T> Page<T> toMpPage(OrderItem... orders){
        // 1.分页条件
        Page<T> p = new Page<>(pageNo, pageSize);
        // 2.排序条件
        // 2.1.先看前端有没有传排序字段
        if (sortBy != null) {
            if (isAsc != null && isAsc) {
                p.addOrder(OrderItem.asc(sortBy));
            } else {
                p.addOrder(OrderItem.desc(sortBy));
            }
            return p;
        }
        // 2.2.再看有没有手动指定排序字段
        if(orders != null){
            p.addOrder(orders);
        }
        return p;
    }

    public <T> Page<T> toMpPage(String defaultSortBy, boolean isAsc){
        if (isAsc) {
            return this.toMpPage(OrderItem.asc(defaultSortBy));
        } else {
            return this.toMpPage(OrderItem.desc(defaultSortBy));
        }
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTimeDesc() {
        return toMpPage(OrderItem.desc("create_time"));
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTimeDesc() {
        return toMpPage(OrderItem.desc("update_time"));
    }
    
    public <T> Page<T> toMpPageDefaultSortByBalanceDesc() {
        return toMpPage(OrderItem.desc("balance"));
    }
}